import Vue from 'vue';
import VueRouter from 'vue-router';
import HomeView from '../views/HomeView.vue';
import { isLogin } from '../utils/tools';

Vue.use(VueRouter);

import Router from 'vue-router';

const originalPush = Router.prototype.push
Router.prototype.push = function push(location,onResolve,onReject) {
if (onResolve || onReject) return originalPush.call(this,location,onResolve,onReject)
return originalPush.call(this,location).catch(err =>err)
}

const routes = [
  {
    path: '/',
    name: 'Home',
    component: HomeView,
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue'),
  },
  {
    path: '/list/:id?',
    name: 'List',
    component: () => import('../views/ListView.vue'),
  },
  {
    path: '/detail',
    name: 'Detail',
    component: () => import('../views/Detail.vue'),
    meta:{
      hideTabBar:true,
    },
  },
  {
    path: '/customers',
    name: 'Customers',
    component: () => import('../views/Customers.vue'),
    
  },
  {
    path: '/shop',
    name: 'Shop',
    component: () => import('../views/Shop.vue'),
    
  },
  {
    path: '/cart',
    name: 'Cart',
    component: () => import('../views/CartView.vue'),
    meta: {
      needLogin: true,
    },
  },
  {
    path: '/user',
    name: 'User',
    component: () => import('../views/UserView.vue'),
    meta: {
      needLogin: true,
    },
  },
  {
    path: '/changePwd',
    name: 'ChangePwd',
    component: () => import('../views/ChangePwd.vue'),
  },
  {
    path: '/changePerInfo',
    name: 'ChangePerInfo',
    component: () => import('../views/ChangePerInfo.vue'),
  },
  {
    path: '/orderdetail',
    name: 'Orderdetail',
    component: () => import('../views/Orderdetail.vue'),
  },
  {
    path: '/reg',
    name: 'Reg',
    component: () => import('../views/Reg.vue'),
    meta: {
      hideTabBar: true,
    },
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue'),
    meta: {
      hideTabBar: true,
    },
  },
  
];
const router = new VueRouter({
  routes,
});
router.beforeEach((to, from, next) => {
  if (to.meta.needLogin) {
    if (isLogin()) {
      next();
    } else {
      next({
        name: 'Login',
      });
    }
  } else {
    next();
  }
});

export default router;
